//
//  MKUITabBarContentViewContainerOneTabView.m
//  MKUI
//
//  Created by sushangjin on 2020/6/5.
//  Copyright © 2020 sushangjin. All rights reserved.
//

#import "MKUITabBarContentContainerOneTabView.h"
#import "MKUITabBarController.h"
@interface MKUITabBarContentContainerOneTabView()
@property(nonatomic,strong) UIView *currentContentView;
@property(nonatomic,strong) id<MKUITabBarContentProtocol> displayContent;
@end

@implementation MKUITabBarContentContainerOneTabView
- (void)setCurrentContentView:(UIView *)contentView{
    if(_currentContentView == contentView) return;
    [_currentContentView removeFromSuperview];
    _currentContentView = contentView;
    _currentContentView.frame = self.bounds;
    [self addSubview:_currentContentView];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    self.currentContentView.frame = bounds;
}
- (void)reloadDataWithAnimated:(BOOL)animated{
    id<MKUITabBarContentProtocol> old = self.displayContent;
    
    id<MKUITabBarContentProtocol> selectedContent = self.selectedContent;
    if(old==selectedContent){
        if([selectedContent respondsToSelector:@selector(tabBarContentViewWillAppear:)]){
            [selectedContent tabBarContentViewWillAppear:animated];
        }
        if([selectedContent respondsToSelector:@selector(tabBarContentViewDidAppear:)]){
            [selectedContent tabBarContentViewDidAppear:animated];
        }
        return;
    }
    if([old respondsToSelector:@selector(tabBarContentViewWillDisapper:)]){
        [old tabBarContentViewWillDisapper:animated];
    }
    if([selectedContent respondsToSelector:@selector(tabBarContentViewWillAppear:)]){
        [selectedContent tabBarContentViewWillAppear:animated];
    }
    if(animated){
        UIView *oldView = self.currentContentView;
        UIView *addView = selectedContent.tabBarContentView;
        CGFloat addViewAlpha = addView.alpha;
        CGFloat oldViewAlpha = oldView.alpha;
        if(addView){
            addView.frame = self.bounds;
            [self addSubview:addView];
            addView.alpha = 0;
        }
        self.displayContent = selectedContent;
        _currentContentView = selectedContent.tabBarContentView;
        [UIView animateWithDuration:0.25 animations:^{
            oldView.alpha = 0;
            addView.alpha = 1;
        } completion:^(BOOL finished) {
            addView.alpha = addViewAlpha;
            oldView.alpha = oldViewAlpha;
            //如果在动画还没结束时，tab又重新选中了oldView。那么在动画结束时，不能移除oldView
            if(oldView==self.currentContentView){
                return;
            }
            [oldView removeFromSuperview];
            
            if([old respondsToSelector:@selector(tabBarContentViewDidDisapper:)]){
                [old tabBarContentViewDidDisapper:animated];
            }
            if([selectedContent respondsToSelector:@selector(tabBarContentViewDidAppear:)]){
                [selectedContent tabBarContentViewDidAppear:animated];
            }
        }];
    }else{
        self.displayContent = selectedContent;
        self.currentContentView = selectedContent.tabBarContentView;
        
        if([old respondsToSelector:@selector(tabBarContentViewDidDisapper:)]){
            [old tabBarContentViewDidDisapper:animated];
        }
        if([selectedContent respondsToSelector:@selector(tabBarContentViewDidAppear:)]){
            [selectedContent tabBarContentViewDidAppear:animated];
        }
    }
    
}
@end

@implementation MKUITabBarController (MKUITabBarContentContainerOneTabView)
- (MKUITabBarContentContainerOneTabView *)oneTabContentContainer{
    if([self.contentContainer isKindOfClass:[MKUITabBarContentContainerOneTabView class]]){
        return self.contentContainer;
    }
    return nil;
}
@end
